package com.android.datn.activities;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.datn.R;
import com.android.datn.adapter.AlbumPagerAdapter;
import com.android.datn.customs.view.CirclePageIndicator;
import com.android.datn.helper.DBBinding;
import com.android.datn.helper.JSonCustomException;
import com.android.datn.objects.Album;
import com.android.datn.utils.CommonUtil;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;

public class AHome extends BaseActivity {
	private ArrayList<Album> mAlbumItems;
	private ViewPager mViewPager;
	private ViewPagerAdapter mAdapter;
	private CirclePageIndicator mIndicator;
	private ProgressDialog mProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		init();
	}

	public void init() {
		mViewPager = (ViewPager) findViewById(R.id.viewpager_album);
		mAlbumItems = new ArrayList<Album>();
		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mAdapter = new ViewPagerAdapter(this, mAlbumItems);
		mProgressDialog = new ProgressDialog(getParent().getParent());
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setMessage("Please wait... Uploading.");
		mProgressDialog.setCancelable(false);
		AlbumAsync ws = new AlbumAsync();
		ws.execute("http://www.mrmai.info/products/musicrank/apis/1.0/get_list_category?");
	}

	public void notification(String message, String title) {
		CommonUtil.dialogNotify(getParent().getParent(), message, title);
	}

	public class ViewPagerAdapter extends AlbumPagerAdapter {
		public ViewPagerAdapter(Activity context, List<Album> albumItems) {
			super(context, albumItems);
		}

		@SuppressWarnings("deprecation")
		@Override
		protected void albumItemClick(int position) {
			// TODO Auto-generated method stub
			Intent t = new Intent(AHome.this, AAlbumItems.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable(DBBinding.ALBUM, mAlbumItems.get(position));
			t.putExtra(DBBinding.BUNDLEALBUM, bundle);
			View view1 = GActivityHome.group
					.getLocalActivityManager()
					.startActivity("FEED",
							t.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			GActivityHome.group.replaceView(view1);
		}
	}

	public class AlbumAsync extends AsyncTask<Object, Void, Object> {

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			JSONObject json = null;
			JSONArray suggestions = null;
			try {
				json = CommonUtil.getJSONFromUrl((String) params[0]);
				suggestions = json.getJSONArray("items");
			} catch (JSonCustomException e) {
				return e.getMessage();
			} catch (JSONException e) {
				return e.getMessage();
			} catch (UnsupportedEncodingException e) {
				return e.getMessage();
			} catch (IOException e) {
				return e.getMessage();
			}
			return suggestions;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			mProgressDialog.show();
		}

		@Override
		protected void onPostExecute(Object result) {
			// TODO Auto-generated method stub
			if (result instanceof JSONArray) {
				try {
					JSONArray suggestions = (JSONArray) result;
					for (int i = 0; i < suggestions.length(); i++) {
						if (i == 64) {
							JSONObject jsonChild;
							jsonChild = suggestions.getJSONObject(i);
							Album album = new Album(Long.parseLong(jsonChild
									.getString("id")));
							album.setImage(jsonChild.getString("picture"));
							album.setListen(jsonChild.getString("listen") == "null" ? "0"
									: jsonChild.getString("song"));
							album.setSong(jsonChild.getString("song") == "null" ? "0"
									: jsonChild.getString("song"));
							album.setTitle(jsonChild.getString("title"));
							album.setVote(jsonChild.getString("vote") == "null" ? "0"
									: jsonChild.getString("vote"));
							mAlbumItems.add(album);
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mViewPager.setAdapter(mAdapter);
				mIndicator.setViewPager(mViewPager);
			} else if (result instanceof String) {
				notification(result.toString(), "Music Online");
			}
			mProgressDialog.dismiss();
		}
	}

}
